namespace GGF
{
    using API;
    using Utility;

    namespace REST
    {
        using System;
        using System.Net;
        using System.IO;
        using System.Text;
        using System.Xml;
        using System.Diagnostics;
        using System.Collections.Generic;

        /// <summary>
        /// Class that creates an HTTP response for XML parsing by GGF.
        /// Constructor for the response takes a corresponding <see cref="GGF.REST.gRequest"/>.
        /// Response requires an object implementing the <see cref="GGF.Utility.IStreamHelper"/>
        /// interface to parse and store returned data from API.
        /// Throws an exception if an errors occurs during response acquisition.
        /// </summary>
        public class gResponse
        {
            #region Fields

            StreamReader readerStream;
            HttpWebResponse response;

            #endregion Fields

            #region Constructors

            /// <summary>
            /// Constructor for a gubb API response over HTTP.
            /// <remarks>
            /// Uses a standard
            /// <see cref="System.Net.HttpWebResponse"/> object to store header
            /// and body.
            /// </remarks>
            /// </summary>
            /// <param name="request">A <see cref="GGF.REST.gRequest"/> which will
            /// use this response object to store the gubb API's response to its
            /// request.</param>
            /// <param name="helper">A class instance implementing <see cref="GGF.Utility.IStreamHelper"/>
            /// which will parse and store data returned in a specific format.</param>
            /// <returns>A response from gubb API which will have either resulted in
            /// an HTTP error or successful population of helper object with user data.</returns>
            /// <exception cref="System.Net.WebException">Thrown when user aborts
            /// request, a time-out is reached, or other error occurred during the call
            /// to the parameter's <see cref="System.Net.HttpWebRequest"/> member to
            /// get a response.</exception>
            public gResponse(gRequest request, IStreamHelper helper)
            {

                try
                {
                    

                    response = (HttpWebResponse)request.getRequestObj().GetResponse();


                    if (request.getRequestObj().HaveResponse == true && response != null)
                        readerStream = new StreamReader(response.GetResponseStream());
                    

                    helper.formatSpecificHelper(readerStream);                    
                    
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine(e.Message);

                    if (e.Response != null)
                    {
                        using (HttpWebResponse errorResponse = (HttpWebResponse)e.Response)
                        {
                            Console.WriteLine("HTTP Error. Description: '{0}' | Status Code: '{1}'", errorResponse.StatusDescription, errorResponse.StatusCode);
                        }

                        throw;
                    }
                }
                finally
                {
                    if (response != null)
                    {
                     
                        response.Close();

                    }
                }

                Debug.WriteLine("API response received and instantiated for request with action id = " + request.getActionId(), "Response creation");
            }

            #endregion Constructors
        }
    }
}